package com.ruoyi.web.controller.admin;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BdmBalanceChange;
import com.ruoyi.system.domain.BdmUser;
import com.ruoyi.system.domain.dto.BdmBalanceChangeDto;
import com.ruoyi.system.domain.dto.BdmQueryCommissionDto;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.BdmUserMapper;
import com.ruoyi.system.service.IBdmBalanceChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

/**
 * 用户 余额 积分 佣金 变动Controller
 *
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@Validated
@RequestMapping("/system/change")
public class BdmBalanceChangeController extends BaseController
{
    @Autowired
    private IBdmBalanceChangeService bdmBalanceChangeService;
    @Autowired
    private BdmUserMapper bdmUserMapper;


    /**
     * 分页 查询 余额 积分
     * @return {@link BdmBalanceChange}
     */
    @GetMapping("/listPage")
    public TableDataInfo list(BdmBalanceChangeDto bdmBalanceChangeDto)
    {
        startPage();
        List<BdmBalanceChange> list = bdmBalanceChangeService.listPage(bdmBalanceChangeDto);
        return getDataTable(list);
    }


    /**
     * 导出用户绑定关系变动列表
     */
    @PostMapping("/exportChange")
    public void export(HttpServletResponse response, BdmBalanceChange bdmBalanceChange)
    {
        List<BdmBalanceChange> list = bdmBalanceChangeService.selectBdmBalanceChangeList(bdmBalanceChange);
        ExcelUtil<BdmBalanceChange> util = new ExcelUtil<BdmBalanceChange>(BdmBalanceChange.class);
        util.exportExcel(response, list, "用户绑定关系变动数据");
    }


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BdmUser> util = new ExcelUtil<BdmUser>(BdmUser.class);
        List<BdmUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        for(BdmUser BdmUser1:userList){
            BdmUser bdmUser= new BdmUser();
            bdmUser.setSysUserid(BdmUser1.getSysUserid());
          //  bdmUser.setTotalWithdraw(bdmTopupRecord.getTopupAmount().toString());
            bdmUser.setCreditScore(BdmUser1.getCreditScore());
            BigDecimal remainSum=   (BdmUser1.getRemainingSum()).add(new BigDecimal(BdmUser1.getTotalTopUp()));
            bdmUser.setRemainingSum(remainSum);
            System.err.println(BdmUser1.getSysUserid()+"---"+remainSum);
             bdmUserMapper.updateWithdraw(bdmUser);
          //  bdmTopupRecord.setStatus("3");
          //  bdmTopupRecord.setType("1");
          //  bdmTopupRecord.setWay("1");
            //bdmBalanceChangeService.insertBdmBalanceChange(bdmBalanceChange);
        }
        //3211 String message = userService.importUser(userList, updateSupport, operName);
        return success("message");
    }


        /**
         * 分页 查询  佣金
         * @return {@link BdmBalanceChange}
         */
    @GetMapping("/listCommission")
    public TableDataInfo listCommission(BdmQueryCommissionDto bdmQueryCommissionDto)
    {
        startPage();
        List<BdmBalanceChange> list = bdmBalanceChangeService.listCommission(bdmQueryCommissionDto);
        return getDataTable(list);
    }



    /**
     * 分页查询会员资产
     * @return {@link BdmBalanceChangeVo}
     */
    @GetMapping("/listMemberAsset")
    public TableDataInfo listMemberAsset(BdmBalanceChangeDto bdmBalanceChangeDto)
    {
        startPage();
        List<BdmBalanceChangeVo> list = bdmBalanceChangeService.listMemberAsset(bdmBalanceChangeDto);
        return getDataTable(list);
    }


    /**
     * 统计余额
     * @return {@link BdmStatisticalBalanceVo}
     */
    @GetMapping("/statisticalBalance")
    public AjaxResult statisticalBalance()
    {
        return success(bdmBalanceChangeService.statisticalBalance());
    }


    /**
     * 统计积分
     * @return {@link BdmStatisticalIntegralVo}
     */
    @GetMapping("/statisticalIntegral")
    public AjaxResult statisticalIntegral()
    {
        return success(bdmBalanceChangeService.statisticalIntegral());
    }


    /**
     * 统计佣金
     * @return {@link BdmStatisticalBrokerageVo}
     */
    @GetMapping("/statisticalBrokerage")
    public AjaxResult statisticalBrokerage()
    {
        return success(bdmBalanceChangeService.statisticalBrokerage());
    }

    /**
     * 查询会员账户资产
     * @return {@link BdmMemberAccountAssetsVo}
     */
    @GetMapping("/queryMemberAccountAssets")
    public AjaxResult queryMemberAccountAssets()
    {
        return success(bdmBalanceChangeService.queryMemberAccountAssets());
    }

    /**
     * 查询 订单概况 支付统计
     * @return {@link BdmOrderProfileAndPaymentStatisticsVo}
     */
    @GetMapping("/queryOrderProfileAndPaymentStatistics")
    public AjaxResult queryOrderProfileAndPaymentStatistics(@NotBlank(message = "年月必传格式 yyyy-MM") String date)
    {
        return success(bdmBalanceChangeService.queryOrderProfileAndPaymentStatistics(date));
    }


    /**
     * 查询 收支趋势 type 1 近七天  2 近一个月
     * @return {@link BdmRevenueAndExpenditureTrendVo}
     */
    @GetMapping("/queryRevenueAndExpenditureTrend")
    public AjaxResult queryRevenueAndExpenditureTrend(@NotNull(message = "类型必选") Long type)
    {
        return success(bdmBalanceChangeService.queryRevenueAndExpenditureTrend(type));
    }

    /**
     * 查询业务类型
     */
    @GetMapping("/queryBusinessType")
    public AjaxResult queryBusinessType()
    {
        return success(bdmBalanceChangeService.queryBusinessType());
    }

    /**
     * 分页 查询 余额 积分
     * @return {@link BdmBalanceChange}
     */
    @GetMapping("/expenditurePage")
    public TableDataInfo expenditurePage()
    {
        startPage();
        List<BdmBalanceChange> list = bdmBalanceChangeService.expenditurePage(1L);
        return getDataTable(list);
    }
}
